package com.ruoyi.web.controller.file;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.my.domain.MyFile;
import com.ruoyi.my.dto.FileModifyDTO;
import com.ruoyi.my.service.MyFileService;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("/file")
public class MyFileController extends BaseController {

    @Autowired
    private MyFileService myFileService;

    /**
     * 获取文件列表
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:list', 'devUnit:organization:list', 'devUnit:technology:list', 'devUnit:money:list', " +
            "'bid:2021:list', 'bid:2022:list', 'bid:2023:list', 'contract:2021:list', 'contract:2022:list', 'contract:2023:list', " +
            "'localSupporting:2021:list', 'localSupporting:2022:list', 'localSupporting:2023:list', 'monitor:2021:list', 'monitor:2022:list', " +
            "'monitor:2023:list', 'performance:2021-2023:list', 'performance:2021:list', 'performance:2022:list', 'performance:2023:list', " +
            "'adaptive:2021:list', 'adaptive:2022:list', 'adaptive:2023:list', 'archives:2021:list', 'archives:2022:list', 'archives:2023:list', " +
            "'others:2021:list', 'others:2022:list', 'others:2023:list', 'devUnit:epc:list'})")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value="bucketName") String bucketName,
                              @RequestParam(value="firstDirectory") String firstDirectory,
                              @RequestParam(value="sectionProjectId") Integer sectionProjectId){
        MyFile myFile = new MyFile();
        myFile.setBucketName(bucketName);
        myFile.setFirstDirectory(firstDirectory);
        myFile.setSectionProjectId(sectionProjectId);
        startPage();
        List<MyFile> myFileList = myFileService.selectFileList(myFile);
        return getDataTable(myFileList);
    }

    /**
     * 查询文件列表
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:query', 'devUnit:organization:list', 'devUnit:technology:list', 'devUnit:money:list', " +
            "'bid:2021:list', 'bid:2022:list', 'bid:2023:list', 'contract:2021:list', 'contract:2022:list', 'contract:2023:list', " +
            "'localSupporting:2021:list', 'localSupporting:2022:list', 'localSupporting:2023:list', 'monitor:2021:list', 'monitor:2022:list', " +
            "'monitor:2023:list', 'performance:2021-2023:list', 'performance:2021:list', 'performance:2022:list', 'performance:2023:list', " +
            "'adaptive:2021:list', 'adaptive:2022:list', 'adaptive:2023:list', 'archives:2021:list', 'archives:2022:list', 'archives:2023:list', " +
            "'others:2021:list', 'others:2022:list', 'others:2023:list', 'devUnit:epc:list'})")
    @GetMapping("/query")
    public TableDataInfo query(MyFile myFile){
        startPage();
        List<MyFile> myFileList = myFileService.selectFileList(myFile);
        return getDataTable(myFileList);
    }

    /**
     * 批量上传文件
     * @param files
     * @param myFile
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:add', 'devUnit:organization:add', 'devUnit:technology:add', 'devUnit:money:add', 'bid:2021:add', " +
            "'bid:2022:add', 'bid:2023:add', 'contract:2021:add', 'contract:2022:add', 'contract:2023:add', 'localSupporting:2021:add', " +
            "'localSupporting:2022:add', 'localSupporting:2023:add', 'monitor:2021:add', 'monitor:2022:add', 'monitor:2023:add', 'performance:2021-2023:add', " +
            "'performance:2021:add', 'performance:2022:add', 'performance:2023:add', 'adaptive:2021:add', 'adaptive:2022:add', 'adaptive:2023:add', " +
            "'archives:2021:add', 'archives:2022:add', 'archives:2023:add', 'others:2021:add', 'others:2022:add', 'others:2023:add', 'devUnit:epc:add'})")
    @PostMapping("/add")
    public AjaxResult add(@RequestParam(value="files") List<MultipartFile> files,
                          MyFile myFile){
        List<String> result = myFileService.insert(files, myFile);
        if (result == null) return AjaxResult.error("上传失败，请检查bucket是否存在");
        return AjaxResult.success(result);
    }

    /**
     * 2022年5月26日
     * 下载文件
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:download', 'devUnit:organization:download', 'devUnit:technology:download', 'devUnit:money:download', " +
            "'bid:2021:download', 'bid:2022:download', 'bid:2023:download', 'contract:2021:download', 'contract:2022:download', 'contract:2023:download', " +
            "'localSupporting:2021:download', 'localSupporting:2022:download', 'localSupporting:2023:download', 'monitor:2021:download', 'monitor:2022:download', " +
            "'monitor:2023:download', 'performance:2021-2023:download', 'performance:2021:download', 'performance:2022:download', 'performance:2023:download', " +
            "'adaptive:2021:download', 'adaptive:2022:download', 'adaptive:2023:download', 'archives:2021:download', 'archives:2022:download', 'archives:2023:download'," +
            "'others:2021:download', 'others:2022:download', 'others:2023:download', 'devUnit:epc:download'})")
    @GetMapping("/download")
    public AjaxResult download(Integer id){
        return AjaxResult.success(myFileService.download(id));
    }

    /**
     * 批量删除文件
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:removes', 'devUnit:organization:rmv', 'devUnit:organization:batchDelete', 'devUnit:technology:rmv', " +
            "'devUnit:technology:batchDelete', 'devUnit:money:rmv', 'devUnit:money:batchDelete', 'bid:2021:rmv', 'bid:2022:rmv', 'bid:2023:rmv', " +
            "'bid:2021:batchDelete', 'bid:2022:batchDelete', 'bid:2023:batchDelete', 'contract:2021:rmv', 'contract:2022:rmv', 'contract:2023:rmv', " +
            "'contract:2021:batchDelete', 'contract:2022:batchDelete', 'contract:2023:batchDelete', 'localSupporting:2021:rmv', 'localSupporting:2022:rmv', " +
            "'localSupporting:2023:rmv', 'localSupporting:2021:batchDelete', 'localSupporting:2022:batchDelete', 'localSupporting:2023:batchDelete', " +
            "'monitor:2021:rmv', 'monitor:2022:rmv', 'monitor:2023:rmv', 'monitor:2021:batchDelete', 'monitor:2022:batchDelete', 'monitor:2023:batchDelete', " +
            "'performance:2021-2023:rmv', 'performance:2021:rmv', 'performance:2022:rmv', 'performance:2023:rmv', 'performance:2021-2023:batchDelete', " +
            "'performance:2021:batchDelete', 'performance:2022:batchDelete', 'performance:2023:batchDelete', 'adaptive:2021:rmv', 'adaptive:2022:rmv', " +
            "'adaptive:2023:rmv', 'adaptive:2021:batchDelete', 'adaptive:2022:batchDelete', 'adaptive:2023:batchDelete', 'archives:2021:rmv', 'archives:2022:rmv', " +
            "'archives:2023:rmv', 'archives:2021:batchDelete', 'archives:2022:batchDelete', 'archives:2023:batchDelete', 'others:2021:rmv', 'others:2022:rmv', " +
            "'others:2023:rmv', 'others:2021:batchDelete', 'others:2022:batchDelete', 'others:2023:batchDelete', 'devUnit:epc:rmv', 'devUnit:epc:batchDelete'})")
    @PostMapping("/removes")
    public AjaxResult remove(@RequestBody List<Integer> ids){
        return AjaxResult.success(myFileService.remove(ids));
    }

    /**
     * 预览文件
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:preview', 'devUnit:organization:view', 'devUnit:technology:view', 'devUnit:money:view', 'bid:2021:view', " +
            "'bid:2022:view', 'bid:2023:view', 'contract:2021:view', 'contract:2022:view', 'contract:2023:view', 'localSupporting:2021:view', " +
            "'localSupporting:2022:view', 'localSupporting:2023:view', 'monitor:2021:view', 'monitor:2022:view', 'monitor:2023:view', 'performance:2021-2023:view', " +
            "'performance:2021:view', 'performance:2022:view', 'performance:2023:view', 'adaptive:2021:view', 'adaptive:2022:view', 'adaptive:2023:view', 'archives:2021:view', " +
            "'archives:2022:view', 'archives:2023:view', 'others:2021:view', 'others:2022:view', 'others:2023:view', 'devUnit:epc:view'})")
    @GetMapping("/preview")
    public AjaxResult preview(Integer id){
        return AjaxResult.success(myFileService.preview(id));
    }




    /**
     * 2022年5月23日
     * 修改文件
     * @param file 修改的新文件
     * @param fileModifyDTO 修改文件的信息
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi({'my:file:preedit', 'devUnit:organization:edit', 'devUnit:technology:edit', 'devUnit:money:edit', 'bid:2021:edit', " +
            "'bid:2022:edit', 'bid:2023:edit', 'contract:2021:edit', 'contract:2022:edit', 'contract:2023:edit', 'localSupporting:2021:edit', " +
            "'localSupporting:2022:edit', 'localSupporting:2023:edit', 'monitor:2021:edit', 'monitor:2022:edit', 'monitor:2023:edit', 'performance:2021-2023:edit', " +
            "'performance:2021:edit', 'performance:2022:edit', 'performance:2023:edit', 'adaptive:2021:edit', 'adaptive:2022:edit', 'adaptive:2023:edit', 'archives:2021:edit', " +
            "'archives:2022:edit', 'archives:2023:edit', 'others:2021:edit', 'others:2022:edit', 'others:2023:edit', 'devUnit:epc:edit'})")
    @PostMapping("/modify")
    public AjaxResult modifyFile(MultipartFile file, FileModifyDTO fileModifyDTO) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return AjaxResult.success(myFileService.modify(file, fileModifyDTO));
    }


    /**
     * 2022年5月26日
     * 获取文件类型列表
     * @return
     */
    @GetMapping("/fetchFileTypeList")
    public AjaxResult fetchFileTypeList(@RequestParam(value = "menu",required = true) String menu,
                                        @RequestParam(value = "name",required = true) String name){
        return AjaxResult.success(myFileService.fetchFileTypes(menu, name));
    }


}
